<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="utf-8"/>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
    <meta http-equiv="X-UA-Compatible" content="IE=edge"/>
    <meta name="viewport" content="width=device-width, initial-scale=1"/>
    <!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->

    <title>sign up</title>
    <link rel="stylesheet" href="../static/assets/bootstrap/css/bootstrap.min.css"
          th:href="@{/assets/sweetalert2/sweetalert2.min.css}"/>
    <link rel="stylesheet" href="../static/assets/sweetalert2/sweetalert2.min.css"
          th:href="@{/assets/bootstrap/css/bootstrap.min.css}"/>
    <style type="text/css">
    </style>
</head>

<body>
<section>
    <div class="container mt-5">
        <div class="row">
            <div class="col-sm-3">

            </div>
            <div class="col-sm-6">
                <form id="signUpForm" action="/signUp" method="post">
                    <h3 class="text-center">注册</h3>
                    <p class="text-center alert alert-danger" th:classappend="${error==null || error==''?'invisible':''}" th:text="${error}"></p>
                    <div class="form-group row">
                        <label for="username" class="col-3 col-form-label text-right">用户名</label>
                        <div class="col-9">
                            <input type="text" name="username" class="form-control" id="username" required="required"
                                   placeholder="用户名"/>
                            <i class="fa fa-user"></i>
                        </div>
                    </div>
                    <div class="form-group row">
                        <label for="password" class="col-3 col-form-label text-right">密　码</label>
                        <div class="col-9">
                            <input type="password" name="password" class="form-control" id="password"
                                   required="required"
                                   placeholder="密　码"/>
                            <i class="fa fa-lock"></i>
                        </div>
                    </div>
                    <div class="form-group row">
                        <label for="confirmPassword" class="col-3 col-form-label text-right">确　认</label>
                        <div class="col-9">
                            <input type="password" name="confirmPassword" class="form-control" id="confirmPassword"
                                   required="required"
                                   placeholder="确认密码"/>
                            <i class="fa fa-lock"></i>
                        </div>
                    </div>
                    <div class="form-group row">
                        <label for="verificationCode" class="col-3 col-form-label text-right">验证码</label>
                        <div class="col-4">
                            <input type="text" name="verificationCode" class="form-control" id="verificationCode"
                                   required="required"
                                   placeholder="验证码"/>
                            <i class="fa fa-lock"></i>
                        </div>
                        <div class="col-5">
                            <img src="/captcha?ts=1" id="captchaImg" style="cursor: pointer;" class="img-fluid"/>
                        </div>
                    </div>
                    <div class="form-group row">
                        <label class="col-3 col-form-label text-right"></label>
                        <div class="col-9">
                            <button type="submit" class="btn btn-primary btn-block">注册</button>
                        </div>
                    </div>
                </form>
            </div>
            <div class="col-sm-3">

            </div>
        </div>
    </div>
</section>
<script src="../static/assets/jquery-3.3.1.min.js"
        th:src="@{/assets/jquery-3.3.1.min.js}"></script>
<script src="../static/assets/bootstrap/js/bootstrap.min.js"
        th:src="@{/assets/bootstrap/js/bootstrap.min.js}"></script>
<script th:src="@{/assets/localforage.min.js}" src="../static/assets/localforage.min.js"></script>
<script th:src="@{/assets/sweetalert2/sweetalert2.min.js}"
        src="../static/assets/sweetalert2/sweetalert2.min.js"></script>
<script th:inline="javascript">
    /*<![CDATA[*/
    $(document).ready(function () {

        function trimBlank(str) {
            return str.replace(/(^\s*)|(\s*$)/g, '')
        }

        $("#captchaImg").on("click", function () {
            var timestamp = (new Date()).valueOf();
            $("#captchaImg").attr("src", "/captcha?ts=" + timestamp);
        })

        $("#signUpForm").on("submit", function (e) {

            if ($("#username").val() == null || trimBlank($("#username").val()) == '') {
                $("#username").focus();
                e.preventDefault();
                return false;
            }

            if ($("#password").val() == null || $("#password").val().trim() == '') {
                $("#password").focus();
                e.preventDefault();
                return false;
            }

            if ($("#confirmPassword").val() == null || $("#confirmPassword").val().trim() == '') {
                $("#confirmPassword").focus();
                e.preventDefault();
                return false;
            }

            if ($("#confirmPassword").val() != $("#password").val()) {
                $("#confirmPassword").focus();
                e.preventDefault();
                return false;
            }

            $.ajax({
                url: "/signUp",
                method: 'post',
                data: $('#signUpForm').serialize(),
                success: function (result) {
                    if (result.status < 0) {
                        Swal('Oops...', result.message, 'error')
                    } else {
                        window.location.href = "/";
                    }
                }
            })
            return false;
        })
    });
    /*]]>*/
</script>

</body>
</html>